<template>
  <div>
    <div class="cate-container">
      <div class="cates-wrap">
        <div class="cates">
          <div v-for="(cate, index) in cates" :key="cate.id" :class="['cate', {
            active: index === activeIndex
          }]"
            @click="switchCate(index)"
          >
            {{ cate.name }}
          </div>
        </div>
      </div>
      <div class="items-wrap">
        <div class="items">
          <div class="item" @click="enterDetail(item.id)" v-for="item in items" :key="item.id">
            <img :src="item.pic" class="item-img" alt="">
            <div class="item-info">
              <h5>{{ item.name }}</h5>
              <div class="item-action">
                <p>￥{{ item.minPrice }}</p>
                <van-icon size="26" class-prefix="icon" name="gouwuchetianjia" />
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <CommonTabbar />
  </div>
</template>

<script setup lang="ts" name="Cate">
import CommonTabbar from '@c/CommonTabbar.vue'
import { fetchCates, fetchItems } from '@a/index'
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router';
const router = useRouter();
const enterDetail = (id:number) => {
  router.push({
    path: '/detail',
    query: {
      id
    }
  })
}
// 所有的分类
const cates = ref<any[]>([]);
// 高亮分类下对应商品
const items = ref<any[]>([]);
// 高亮下标
const activeIndex = ref<number>(0);

// 获取所有分类
const getCates = () => {
  fetchCates().then(res => {
    if (res.data.code === 0) {
      cates.value = res.data.data
      // 初始获取第一个分类下的所有的商品
      getItems()
    }
  })
}
// 对应分类商品
const getItems = () => {
  fetchItems({
    categoryId: cates.value[activeIndex.value].id
  }).then(res => {
    if (res.data.code === 0) {
      items.value = res.data.data.result
    }
    if (res.data.code === 700) {
      items.value = []
    }
  })
}
// 点击分类切换高亮状态以及 分类下的商品
const switchCate = (index: number) => {
  activeIndex.value = index
  getItems()
}
onMounted(() => {
  getCates()
})
</script>

<style scoped lang="scss">
.cate-container {
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  bottom: 50px;
  background-color: #fff;
  display: flex;

  .cates-wrap {
    width: 3rem;
    height: 100%;
    overflow: auto;
    border-right: 1px solid #ddd;

    .cate {
      line-height: 40px;
      text-align: center;
      font-size: 16PX;
      position: relative;

      &.active {
        color: #e93323;

        &:after {
          content: '';
          display: block;
          width: 3px;
          height: 40px;
          background-color: #e93323;
          position: absolute;
          left: 0;
          top: 0;
        }
      }
    }
  }

  .items-wrap {
    width: 7rem;
    height: 100%;
    overflow: auto;
    .item {
    display: flex;
    padding: 10px;
    background-color: #fff;
    margin-top: 10px;
    img {
      width: 120px;
    }
    .item-info {
      flex: 1;
      padding: 0 10px;
      h5 {
        line-height: 40px;
      }
      .item-action {
        display: flex;
        justify-content: space-between;
        p {
          font-size: 20PX;
          color: #ec1109;
        }

      }
    }
  }
  }
}




</style>